package com.xiangban.yanglaodajie_platform.service;

import com.xiangban.yanglaodajie_platform.dao.ClassifyGoodsMapper;
import com.xiangban.yanglaodajie_platform.dao.GoodsMapper;
import com.xiangban.yanglaodajie_platform.entity.ClassifyGoods;
import com.xiangban.yanglaodajie_platform.entity.Goods;
import com.xiangban.yanglaodajie_platform.entity.ShoppingCart;
import com.xiangban.yanglaodajie_platform.entity.dto.GoodsDto;
import com.xiangban.yanglaodajie_platform.entity.dto.ShoppingCartDto;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_platform.exception.BizException;
import com.xiangban.yanglaodajie_platform.utils.CommonUtils;
import com.xiangban.yanglaodajie_platform.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.xiangban.yanglaodajie_platform.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/3/15 16:57
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ClassifyGoodsMapper classifyGoodsMapper;



    public Object getPage(ShoppingCart shoppingCart) {
        ClassifyGoods goods2 = new ClassifyGoods();
        goods2.setShopId(shoppingCart.getShopId());
        List<ClassifyGoods> classifyGoods = classifyGoodsMapper.getList(goods2);
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (ClassifyGoods goodsClassify : classifyGoods) {
            HashMap<String, Object> map1 = new LinkedHashMap<>();
            ArrayList<Object> list = new ArrayList<>();
            if (commonUtils.getUserId(request) != null) {
                ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
                shoppingCartDto.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
                shoppingCartDto.setClassifyId(goodsClassify.getId());
                shoppingCartDto.setShopId(shoppingCart.getShopId());
                List<GoodsDto> goods1 = goodsMapper.findGoods(shoppingCartDto);
                //商家里商品表关联购物车取number
                if (goods1.size ()>0){
                    map1.put("id",goodsClassify.getId());
                    map1.put("classifyName", goodsClassify.getClassifyName());
                    if (goods1 != null) {
                        for (GoodsDto goodsDto : goods1) {
                            if (goodsDto.getStatus().equals("1") && goodsDto.getInventory() > 0) {
                                list.add(goodsDto);
                            }
                        }
                    }
                    map1.put("data", list);
                    maps.add(map1);
                }

            } else {
                ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
                shoppingCartDto.setClassifyId(goodsClassify.getId());
                shoppingCartDto.setShopId(shoppingCart.getShopId());
                List<GoodsDto> goods = goodsMapper.getGoods(shoppingCartDto);
                if (goods.size ()>0) {
                    map1.put("id", goodsClassify.getId());
                    map1.put("classifyName", goodsClassify.getClassifyName());
                    if (goods != null) {
                        for (GoodsDto goodsDto : goods) {
                            if (goodsDto.getStatus().equals("1") && goodsDto.getInventory() > 0) {
                                list.add(goodsDto);
                            }
                        }
                    }
                    map1.put("data", list);
                    maps.add(map1);
                }
            }
        }
        return maps;
    }



    public void alterGoods(Goods goods) {//参数：上下架||库存
        if (BizEmpty(goods.getStatus())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if(BizEmpty(goods.getInventory())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        goods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        goodsMapper.updateGoodsInfo(goods);
    }

}
